package com.afsadmedia.pregnancy;

import java.util.ArrayList;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import java.util.List;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;

public class WeightManagement extends SherlockActivity {
	
	
	List<ListItemWeight> rowItems;
	 CustomListViewAdapterWeightMang adapter;
	 
	 ArrayList<String> adddate, addweight;
	 
	ListView listView;
	TextView statictext;
	
	
//	@Override
//	protected void onStart() {
//		// TODO Auto-generated method stub
//		statictext.setText("");
//		super.onStart();
//	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weightmanagement);
		
 
		
		
		/////////
//		adddate = new ArrayList<String>();
//		addweight = new ArrayList<String>();
//		
//		adddate.add(Constants.calculatedate);
//		addweight.add(Constants.selectweight);
		
		if(Constants.addedweight!= null){
			
	   
		
		rowItems = new ArrayList<ListItemWeight>();
		for (int i = 0; i < Constants.addedweight.size(); i++) {

			ListItemWeight item1 = new ListItemWeight(Constants.addeddate.get(i), Constants.addedweight.get(i));

			rowItems.add(item1);
		}

		listView = (ListView) findViewById(R.id.listView_weightmanagemnt);
		
		adapter = new CustomListViewAdapterWeightMang(getApplicationContext(), R.layout.listitem_weightmang,
				rowItems);
		listView.setAdapter(adapter);
		
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub

				AlertDialog.Builder adb = new AlertDialog.Builder(WeightManagement.this);
				adb.setTitle("Delete?");
				adb.setMessage("Are you sure you want to delete the Selected Entery."
						);

				final int positionToRemove = position;

				adb.setNegativeButton("Cancel", null);
				adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					
						Constants.addedweight.remove(positionToRemove);
						Constants.addeddate.remove(positionToRemove);
						
						adapter.notifyDataSetChanged();

						listView.setAdapter(adapter);
						listView.invalidateViews();

						Intent i = new Intent(WeightManagement.this, WeightManagement.class);
						startActivity(i);
					}
				});
				adb.show();

			
				return true;
			}
		});
		
		
		}
		
		
		
		
		
	}
	
	
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) 
	{
		getSupportActionBar().setBackgroundDrawable(
				getResources().getDrawable(R.drawable.actionbar_background)); 
		getSupportActionBar().setTitle("Favorite");
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);
		
	
		menu.add(Menu.NONE, R.id.add, Menu.NONE,R.string.toggle_show_home)
	    .setIcon(R.drawable.addicon)
	    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
	    return super.onCreateOptionsMenu(menu);
 
    }
	
	
	
	public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
	    switch(item.getItemId()){
	    
	    case android.R.id.home:
	    	
//			Intent in=new Intent(Details.this,FavItems.class);
//			in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			startActivity(in);
    	
        return true;
	   
	    case R.id.add:
	    	
	    	Intent i = new Intent(WeightManagement.this,Add_WeightManagement.class);
     		startActivity(i);
	        return true;
	    default:
	        return false;
	    }
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent i = new Intent(WeightManagement.this,Listview.class);
		startActivity(i);
		super.onBackPressed();
	}

}
